package com.zhang.util;

import com.zhang.study.chapter03.node.DoubleNode;
import com.zhang.study.chapter03.node.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhang
 * @date 2023/11/22 17:52
 * 链表工具类
 */
public class NodeUtil {


    /**
     * 生成一个随机的链表
     *
     * @param len   链表长度
     * @param value 链表值
     * @return 链表头结点
     */
    public static Node generateRandomLinkedList(int len, int value) {
        int size = (int) (Math.random() * (len + 1));
        if (size == 0) {
            return null;
        }
        size--;
        Node head = new Node((int) (Math.random() * (value + 1)));
        Node pre = head;
        while (size != 0) {
            Node cur = new Node((int) (Math.random() * (value + 1)));
            pre.next = cur;
            pre = cur;
            size--;
        }
        return head;
    }

    /**
     * 将链表转换成List
     *
     * @param head 链表头结点
     * @return list
     */
    public static List<Integer> getLinkedListOriginOrder(Node head) {
        List<Integer> ans = new ArrayList<>();
        while (head != null) {
            ans.add(head.value);
            head = head.next;
        }
        return ans;
    }

    /**
     * 判断链表是否是反转的
     *
     * @param origin 链表值的list
     * @param head   链表的反转顺序
     * @return 是否是反转的
     */
    public static boolean checkLinkedListReverse(List<Integer> origin, Node head) {
        for (int i = origin.size() - 1; i >= 0; i--) {
            if (!origin.get(i).equals(head.value)) {
                return false;
            }
            head = head.next;
        }
        return true;
    }

    /**
     * 生成一个随机的双向链表
     *
     * @param len   链表长度
     * @param value 链表最大值
     * @return 链表头结点
     */
    public static DoubleNode generateRandomDoubleList(int len, int value) {
        int size = (int) (Math.random() * (len + 1));
        if (size == 0) {
            return null;
        }
        size--;
        DoubleNode head = new DoubleNode((int) (Math.random() * (value + 1)));
        DoubleNode pre = head;
        while (size != 0) {
            DoubleNode cur = new DoubleNode((int) (Math.random() * (value + 1)));
            pre.next = cur;
            cur.previous = pre;
            pre = cur;
            size--;
        }
        return head;
    }

    /**
     * 将双向链表转换成List
     *
     * @param head 链表头结点
     * @return list
     */
    public static List<Integer> getDoubleListOriginOrder(DoubleNode head) {
        List<Integer> ans = new ArrayList<>();
        while (head != null) {
            ans.add(head.value);
            head = head.next;
        }
        return ans;
    }

    /**
     * 判断双向链表是否是反转的
     *
     * @param origin 翻转前的链表值的list
     * @param head   翻转后的头结点
     * @return 是否是反转的
     */
    public static boolean checkDoubleListReverse(List<Integer> origin, DoubleNode head) {
        DoubleNode end = null;
        for (int i = origin.size() - 1; i >= 0; i--) {
            if (!origin.get(i).equals(head.value)) {
                return false;
            }
            end = head;
            head = head.next;
        }
        for (Integer integer : origin) {
            if (!integer.equals(end.value)) {
                return false;
            }
            end = end.previous;
        }
        return true;
    }


}
